﻿//     Copyright (c) Microsoft Corporation.  All rights reserved.

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Gateway.Configuration.Converter;
using Microsoft.Gateway.Utils;
using Newtonsoft.Json;

namespace Microsoft.Gateway.Configuration
{
    public enum FlightPivots
    {
        Default = 0, 
        User = 1,
        Tenant = 2,
        SourceIP = 3,
        DomainName = 4
    }

    public class Flight 
    {
        [JsonProperty(Order=0)]
        public string Name { get; set; }

        [JsonConverter(typeof(StringToCollectionConverter))]
        public List<string> SourceIP { get; set; }

        [JsonConverter(typeof(StringToCollectionConverter))]
        public List<string> User { get; set; }

        [JsonConverter(typeof(StringToCollectionConverter))]
        public List<string> Tenant { get; set; }

        [JsonConverter(typeof(TargetRouteConverter))]
        public List<TargetRoute> Target { get; set; }
        
        [JsonConverter(typeof(StringToCollectionConverter))]
        public List<string> DomainName { get; set; }        

        public string GetEffectiveRedirectBase(Role containingRole)
        {
            if (IsArrFlight)
            {
                return string.Format("{0}{1}", containingRole.Segment, Name);
            }
            else if (Target != null && Target.Count == 1)
            {
                // it is not an arr flight
                if (!string.IsNullOrWhiteSpace(Target.First().Redirect))
                {
                    return Target.First().Redirect;
                }
                else
                {
                    return containingRole.GetEffectiveRedirectBase(); 
                }
            }               
            else
            {
                return containingRole.GetEffectiveRedirectBase();
            }
        }

        [JsonIgnore]
        public bool IsArrFlight
        {
            get { return Target != null && Target.Any() && Target.Count > 1; }
        }
    }
}
